// pages/user/map/map.js
import { Target, HttpUtil } from '../../../utils/http_util.js'
var config = require('../../../utils/config.js')
var QQMapWX = require('../../../lib/qqmap-wx-jssdk.js')
var qqmapssdk = new QQMapWX({
  key: 'XEFBZ-BFQKG-QCXQM-IUH47-AZMI7-BHBIP'
})
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url:'',
    userInfo:'',

    latitude: 0,//地图初次加载时的纬度坐标
    longitude: 0, //地图初次加载时的经度坐标
    name: "" //选择的位置名称
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      url:options.url
    })

    wx.getStorage({
      key: 'locationInfo',
      success(lores) {
        if (lores === null || lores.data === null || lores.data.lid === null || lores.data.lid === '') {
        } else {
          that.setData({
            latitude: lores.data.latitude,
            longitude: lores.data.longitude
          })
        }
      }
    })

    wx.getStorage({
      key: 'userInfo',
      success(res) {
        that.setData({
          userInfo: res.data
        });
      }
    })

    this.moveToLocation();
  },

  //移动选点
  moveToLocation: function () {
    var that = this;
    wx.chooseLocation({
      success: function (res) {
        console.log(res);
        //选择地点之后返回到原来页面
        var address = res.address +' '+ res.name;
        // wx.navigateTo({
        //   url: "/pages/user/addAddress/addAddress?address=" + address
        //        + "&latitude=" + res.latitude +"&longitude="
        //        + res.longitude
        // });

            var upAddrParam = {
              id: that.data.userInfo.id,
              serviceArea: address,
              latitude: res.latitude,
              longitude: res.longitude
            }
            HttpUtil.get(Target.self, "artificerMeans/update", upAddrParam).then(latres => {
              console.log("技师地址已更新！")
              var info = that.data.userInfo;
              info.serviceArea = address;
              info.latitude = res.latitude;
              info.longitude = res.longitude;
              wx.setStorageSync('userInfo', info)

              wx.reLaunch({
                url: that.data.url
              })
            })

          
      },
      fail: function (err) {
        wx.navigateTo({
          url: that.data.url
        });
      }
    });
  }

})